package com.service.edu.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.edu.listenter.ExcelSubjectReadListenter;
import com.service.edu.pojo.Subject;
import com.service.edu.pojo.Teacher;
import com.service.edu.pojo.excel.SubjectData;
import com.service.edu.pojo.vo.TeacherQuery;
import com.service.edu.service.SubjectService;
import com.service.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-24
 */
@RestController
@RequestMapping("subject")
@Api(description = "课程分类管理")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @GetMapping
    @ApiOperation("查询所有课程分类信息")
    public ResultVo list(){

        return ResultVo.ok().data("items",subjectService.getList());
    }

    /**
     * 查询父分类id查询分类信息
     * @param pid
     * @return
     */
    @ApiOperation("查询父分类id查询分类信息")
    @GetMapping("{pid}")
    public  ResultVo queryByPid(@ApiParam(name = "pid",value = "查询父分类id查询分类信息",required = true) @PathVariable String pid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",pid);
        List list = subjectService.list(queryWrapper);
        return ResultVo.ok().data("item",list);
    }

    /**
     * 删除的课程分类信息(一个或多个)
     * @param ids
     * @return
     */
    @ApiOperation("逻辑删除课程分类信息")
    @DeleteMapping("{ids}")
    public ResultVo delete(@ApiParam(name = "ids",value = "要删除的课程分类ID(一个或多个)",required = true) @PathVariable List<String> ids){
        boolean b = subjectService.removeByIds(ids);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 修改课程分类信息
     * @param Subject
     * @return
     */
    @ApiOperation("修改课程分类信息")
    @PutMapping
    public ResultVo update(@RequestBody Subject Subject){
        Subject.setGmtModified(new Date());
        boolean b = subjectService.updateById(Subject);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 添加课程分类信息
     * @param Subject
     * @return
     */
    @ApiOperation("添加课程分类信息")
    @PostMapping
    public ResultVo save(@RequestBody Subject Subject){
        Subject.setGmtModified(new Date());
        Subject.setGmtCreate(new Date());
        boolean b = subjectService.save(Subject);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 上传excel读取课程分类信息    添加课程分类信息
     * @param file
     * @return
     */
    @ApiOperation("上传excel读取课程信息添加课程分类信息")
    @PostMapping("excelSave")
    public ResultVo save(@ApiParam(name = "file",value = "上传excel课程分类信息",required = true) @RequestParam("file") MultipartFile file) {
        try {
            //读取excel
            EasyExcel.read(file.getInputStream(), SubjectData.class,new ExcelSubjectReadListenter(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultVo.ok();
    }

}

